<template>
	<view class="mainbgc">
		<view class="mainpadding">
			<view class="flexbetween1">
				<view class="titletext">收货人</view>
				<input class="inputs" type="text" placeholder="请输入收货人姓名"  v-model="name">
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">手机号码</view>
				<input class="inputs" type="text" placeholder="请输入收货人手机号" v-model="mobile">
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">选择地址</view>
				  <view class="flexbetween xiahuaxian">
					<uni-data-picker  @change="bindPickerChange" :localdata="items"  placeholder="请选择住址" popup-title="请选择地区" v-model="address.county" 
					class="zdy">
						<text class="inputs1" v-if="!area.length" style="color: #999999; font-size:30rpx;">请选择住址</text>
						<text class="inputs1" v-else>{{ area[0] }}/{{ area[1] }}/{{ area[2] }}</text>
					</uni-data-picker>
					<view class="right_icon"></view>
				</view>   
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">详细地址</view>
				<input class="inputs" type="text" placeholder="街道、楼牌号等" v-model="addressdetail">
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">设为默认地址</view>
				 <view class="">
					<u-switch space="2" v-model="value11" activeColor="#248dfe" inactiveColor="rgb(230, 230, 230)">
					</u-switch>
				</view>  
			</view>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb" @click="addaddress">
			<view class="bigbtn">保存</view>
		</view>
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "../../static/city/city.js"
	export default {
		data() {
			return {
				area:"",
				listid: "",
				status:"",
				addressdetail:"",//详细地址
				address:{
					province: "", //省
					city: "", //市
					county: "", //区
				},
				name:"",//姓名
				mobile:"",//收货人电话
				data:{},
				titletext: "新增收货地址",
				value11:false,
				items: citys.data,
			}
		},
		onLoad(options) {
		 this.listid = options.id;
		 if (this.listid) {
		 	this.titletext = "编辑收货地址"
		 } else {
		 	this.titletext = "新增收货地址"
		 }
		},
		onShow() {
		this.dizhixq()
		},
		methods: {
			isMobile(value) {//手机号校验
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
		// 修改地址接口
		xiugdz(){
			let _this=this;
			httpRequest.request('/api/user/editAddress', 'GET', {
				// 传参
				id: _this.listid,
				name: _this.name,
				mobile: _this.mobile,
				address_detail: _this.addressdetail,
				status: _this.value11 ? 1 : 0,
				province_id: this.address.province,
				city_id: _this.address.city,
				area_id: _this.address.county
			}, false, false, true).then(res => {
				uni.hideLoading()
				httpRequest.toast(res.msg)
				if (res.code == 1) {
					setTimeout(() => {
						uni.navigateBack(1)
					}, 1000)
				}
			})
		},
			 // 地址详情接口
	  	 	 dizhixq() {
			 	let _this=this;
			 	httpRequest.request('/api/user/addressDetail', 'GET', {
			 		// 传参
			 		id: _this.listid
			 	}, false, false, true).then(res => {
			 		if (res.code == 1) {
			 			 this.area = res.data.cityinfo.split("/")
			 			this.address.province = res.data.province_id
			 			this.address.city = res.data.city_id
			 			this.address.county = res.data.area_id
			 			this.name = res.data.name
			 			this.mobile = res.data.mobile
			 			this.addressdetail = res.data.address_detail
			 		  	if (res.data.status == 1) {//判断是否默认按钮
			 				this.value11 = true
			 			} else {
			 				this.value11 = false
			 			}  
			 		}
			 	})
			 }, 
			 addaddress(){ //添加地址
				uni.showLoading({
					title: '请稍候...',
					mask:true
				})
				let _this = this;
					let mobile = _this.mobile;
				if (_this.name == '') {
					uni.hideLoading()
					httpRequest.toast('请输入收货人名字');	
					return false;
				}
				if (_this.mobile == "") {
					uni.hideLoading()
					httpRequest.toast('请输入收货人手机号');
					return false;
				}
				if (!_this.isMobile(_this.mobile)) {
					uni.hideLoading()
					httpRequest.toast("手机号输入有误")
					return false
				}
				if (_this.area == false) {
					uni.hideLoading()
					httpRequest.toast('请选择地址');
					return false;
				}
				if (_this.addressdetail == false) {
					uni.hideLoading()
					httpRequest.toast('请输入详细地址');
					return false;
				}
				if(_this.listid){
				 	_this.xiugdz() 
				}else{
					httpRequest.request('/api/user/addAddress', 'GET', {//添加地址
						// 传参
						name: _this.name,
						mobile: _this.mobile,
						address_detail:_this.addressdetail,
						status:_this.value11 ? 1 : 0,
					    province_id: _this.address.province,
					    city_id:_this.address.city,
					    area_id: _this.address.county
					}, false, false, true).then(res => {
						uni.hideLoading()
						httpRequest.toast(res.msg)
						if (res.code == 1) {
							setTimeout(() => {
								uni.navigateBack(1)
							}, 1000)
						}
					})
				}
			 },
			 bindPickerChange(val) {
			 	let area = val.detail.value
			 	this.area = [area[0].text, area[1].text, area[2].text];
			 	this.address.province = area[0].value;
			 	this.address.city = area[1].value;
			 	this.address.county = area[2].value;
			 },
		}
	}
</script>

<style lang="scss" scoped>
	.xiahuaxian {
		width: 72%;
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid #F1F1F1;
	}
	.zdy{
		width: 100%;
	}

	.flexbetween1 {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		padding: 10rpx 0;
	}

	.inputs1 {
		width: 100%;
	}

	.inputs {
		width: 72%;
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid #F1F1F1;
	}
	/deep/.selected-item-active{
	      border-bottom: 2px solid #ffc059;
	 }
</style>
